package com.lr.springbootmybatisdemo.service.impl;

import com.lr.springbootmybatisdemo.dao.MallUserDao;
import com.lr.springbootmybatisdemo.dto.MallUserAndOrderDTO;
import com.lr.springbootmybatisdemo.entity.MallOrder;
import com.lr.springbootmybatisdemo.dao.MallOrderDao;
import com.lr.springbootmybatisdemo.entity.MallUser;
import com.lr.springbootmybatisdemo.service.MallOrderService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (MallOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-10-24 14:32:19
 */
@Service("mallOrderService")
public class MallOrderServiceImpl implements MallOrderService {
    @Resource
    private MallOrderDao mallOrderDao;

    @Resource
    private MallUserDao mallUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MallOrder queryById(Integer id) {
        return this.mallOrderDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<MallOrder> queryAllByLimit(int offset, int limit) {
        return this.mallOrderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param mallOrder 实例对象
     * @return 实例对象
     */
    @Override
    public MallOrder insert(MallOrder mallOrder) {
        this.mallOrderDao.insert(mallOrder);
        return mallOrder;
    }

    /**
     * 修改数据
     *
     * @param mallOrder 实例对象
     * @return 实例对象
     */
    @Override
    public MallOrder update(MallOrder mallOrder) {
        this.mallOrderDao.update(mallOrder);
        return this.queryById(mallOrder.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.mallOrderDao.deleteById(id) > 0;
    }

    /**
     * 通过用户id获取用户信息和订单列表(倒序展示)
     *
     * @param userId
     * @return
     */
    @Override
    public MallUserAndOrderDTO queryOrderInfoByUserId(Long userId) {
        //1.通过用户id获取用户信息
        MallUser mallUser = new MallUser();
        mallUser.setUserId(userId);
        List<MallUser> mallUserList = mallUserDao.queryAll(mallUser);
        if (CollectionUtils.isEmpty(mallUserList)) {
            return null;
        }
        MallUser user = mallUserList.get(0);
        //2.根据用户id查询订单列表
        MallOrder mallOrder = new MallOrder();
        mallOrder.setUserId(userId);
        List<MallOrder> mallOrderList = mallOrderDao.queryAll(mallOrder);
        //根据购买时间倒序展示
        List<MallOrder> orderList = mallOrderList.stream()
                .sorted(Comparator.comparing(MallOrder::getCreateTime).reversed())
                .collect(Collectors.toList());


        //封装返回对象
        return MallUserAndOrderDTO.builder().userId(userId)
                .userAddress(user.getUserAddress())
                .userBirthday(user.getUserBirthday())
                .userName(user.getUserName())
                .userGender(user.getUserGender())
                .userPhone(user.getUserPhone())
                .mallOrderList(orderList)
                .build();
    }
}